#include "customNaviButton.h"
#include "highlight-effect.h"
#include "imageutil.h"
#include <QDebug>

CustomNaviButton::CustomNaviButton(int name,QWidget *parent)
    :QPushButton(parent)
{
    setFixedSize(180,40);
    setFlat(true);
    this->setCheckable(true);

    m_strIcon = "";
    m_strText = "";


    setProperty("useButtonPalette", true);
    initStyleTheme();
    initUI(name);

    this->setObjectName("btn");
    this->setParent(parent);

}

void CustomNaviButton::initUI(int name)
{
    mp_wholeHL = new QHBoxLayout(this);

    mp_btnName = new QLabel(this);
    mp_btnName->setObjectName("name");
    mp_btnName->setFixedHeight(28);
    mp_btnName->setAlignment(Qt::AlignVCenter|Qt::AlignLeft);

    mp_btnIcon = new QLabel();
    mp_btnIcon->setObjectName("icon");
    switch (name) {
    case static_cast<int>(TabName::INTRO):
        m_strIcon = ":/data/view-dual-symbolic.svg";
        m_strText = "Intro";
        mp_btnName->setText(tr("Intro"));
        break;
    case static_cast<int>(TabName::MESSAGE):
        m_strIcon = ":/data/document-edit-symbolic.svg";
        m_strText = "Message";
        mp_btnName->setText(tr("Message"));
        break;
    case static_cast<int>(TabName::CONTACT):
        m_strIcon = ":/data/call-start-symbolic.svg";
        m_strText = "Contact";
        mp_btnName->setText(tr("Contact"));
        break;
    case static_cast<int>(TabName::GUIDANCE):
        m_strIcon = ":/data/system-search-symbolic.svg";
        m_strText = "Guidance";
        mp_btnName->setText(tr("Guidance"));
        break;
    case static_cast<int>(TabName::CUSTOMER):
        m_strIcon = ":/data/ukui-headset-symbolic.svg";
        m_strText = "Customer";
        mp_btnName->setText(tr("Customer"));
        break;
    default:
        break;
    }
    installEventFilter(this);
    mp_btnIcon->setFixedSize(QSize(16, 16));
    mp_btnIcon->setFocusPolicy(Qt::NoFocus);
    mp_btnIcon->update();


    mp_wholeHL->setMargin(0);
    mp_wholeHL->setSpacing(0);
    mp_wholeHL->addSpacing(16);
    mp_wholeHL->addWidget(mp_btnIcon);
    mp_wholeHL->addSpacing(8);
    mp_wholeHL->addWidget(mp_btnName);
    mp_wholeHL->addStretch();

    this->setLayout(mp_wholeHL);

    setIcon(m_strIcon);

    connect(this, &QPushButton::toggled, this, [=](bool checked) {
        if (checked) {
            setStyleSheet("QPushButton:checked{background-color: palette(highlight);border-radius: 6px;}");
            mp_btnName->setStyleSheet("color:white");
            m_isNormal = false;
        } else {
            mp_btnName->setStyleSheet("color:palette(windowText)");
            if (!m_isHover) {
                m_isNormal = true;
            }
        }
        setIcon(m_strIcon);
    });
}

bool CustomNaviButton::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == this) {
        switch (event->type()) {
        case QEvent::HoverEnter:
            m_isHover = true;
            setIcon(m_strIcon);
            break;
        case QEvent::HoverLeave:
            m_isHover = false;
            setIcon(m_strIcon);
            break;
        default:
            break;
        }
    }
    return QPushButton::eventFilter(obj, event);
}

void CustomNaviButton::initStyleTheme()
{
    const QByteArray idd(THEME_QT_SCHEMA);
    if(QGSettings::isSchemaInstalled(idd)) {
        m_styleSettings = new QGSettings(idd);
    }
    if (m_styleSettings) {
        connect(m_styleSettings, &QGSettings::changed, this, [=](const QString &key) {
            if (key == "styleName") {
                auto styleNameValue = m_styleSettings->get("styleName");
                if (styleNameValue.isValid()) {
                    m_strThemeName = styleNameValue.toString();
                    setIcon(m_strIcon);
                }
            }
        });
        auto styleNameValue = m_styleSettings->get("styleName");
        if (styleNameValue.isValid()) {
            m_strThemeName = styleNameValue.toString();
        }
    }
}
void CustomNaviButton::setIcon(QString &strIcon)
{
    m_strIcon = strIcon;
    QPixmap pixmap;
    if (m_strThemeName == "ukui-dark" || m_strThemeName == "ukui-black") {
        if (m_isNormal) {
            pixmap = ImageUtil::loadSvg(m_strIcon, "white", 16);
        } else {
            pixmap = ImageUtil::loadSvg(m_strIcon, "white", 16);
        }
    } else { // "ukui-default" "ukui-white" "ukui-light" "ukui"
        if (m_isNormal) {
            pixmap = ImageUtil::loadSvg(m_strIcon, "black", 16);
        } else {
            pixmap = ImageUtil::loadSvg(m_strIcon, "white", 16);
        }
    }
    if (!pixmap.isNull()) {
        mp_btnIcon->setPixmap(pixmap);
        mp_btnIcon->show();
    } else {
        mp_btnIcon->hide();
    }
}
void CustomNaviButton::changeFont(QFont curFont)
{
    mp_btnName->setFont(curFont);
}
